'use strict';

const RequestJsonApi = require('../lib/class/requestJsonApi');

module.exports = app => {
  // 定义参数 继承request类

  const host = app.config.apiServiceLocation.order;
  const url = '/orders';
  const dataType = 'orders';

  const orders = new Orders({ host, url, dataType });

  return orders;
};

class Orders {
  constructor (options) {
    this.requestJsonApi = new RequestJsonApi(options);
  }
  async getListOrders (filters, pages) {
    const sort = '-id';
    const data = await this.requestJsonApi.get({ filters, pages, fields: { products: 'name,logo,prices,productType' }, sort });
    return data;
  }
  async createOrder (userId, shopId, comment, productList, systemComment) {
    const data = await this.requestJsonApi.post({ userId, shopId, comment, productList, systemComment });
    return data;
  }
  async getOrderDetail (idList) {
    const data = await this.requestJsonApi.get({ idList });
    return data;
  }
  async changeOrderPay (id, payType, payComment) {
    const attributes = {
      payType,
      payComment
    };
    const path = '/' + id + '/pay';

    const data = await this.requestJsonApi.put(attributes, { path });
    return data;
  }
}
